<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;

class Appointment extends  Model
{

    const status0 = '待审核';
    const status1 = '待核销';
    const status2 = '已核销';

    use HasFactory;
    use SoftDeletes;

    static $unguarded = true;

    protected $table = 'appointment';

    /**
     * 根据ID获取数据
     *
     * @param $id
     * @param mixed $columns
     * @return array|Builder
     */
    public static function getDataByID($id, string $columns = ''){
        $data = self::query();
        if($columns){
            $data->find($id,$columns);
            return $data;
        }
        $val = $data->find($id);
        $status_show =  self::getStatusValue($val['status']);
        return [
            'id'=>$val['id'],
            'user_id'=>$val['user_id'],
            'username'=>$val['username'],
            'mobile'=>$val['mobile'],
            'status'=>$val['status'],
            'status_show'=>$status_show,
            'id_card'=>$val['id_card'],
            'enter_at'=>Carbon::parse($val['enter_at'])->toDateTimeString(),
            'actual_enter_at'=>Carbon::parse($val['actual_enter_at'])->toDateTimeString(),
            'number'=>$val['number'],
            'school_gate_id'=>$val['school_gate_id'],
            'school_gate_name'=>SchoolGate::query()->where('id',$val['school_gate_id'])->value('name'),
            'created_at'=>Carbon::parse($val['created_at'])->toDateTimeString(),
            'updated_at'=>Carbon::parse($val['updated_at'])->toDateTimeString(),
       ];
    }

    /**
     * 预约核销状态
     *
     * @param $status
     * @return string
     */
    public static function getStatusValue($status): string
    {
        $status_value = self::status2;
        if($status == 1){
            $status_value = self::status1;
        }
        if($status == 0){
            $status_value = self::status0;
        }
        return $status_value;
    }
}
